﻿using CSM_SQL;
using CSM_SQL.BLL;
using Newtonsoft.Json;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace CommunitySuperMarketManager.Controllers
{
    public class CustomerController : Controller
    {
        // GET: Customer
        public ActionResult Customer()
        {
            var e = (Emp)HttpContext.Session["CurrUser"];
            if (e != null)
            {
                ViewBag.CurrUser = e;
                if (e.Auth > 0)
                    return View();
                else
                    return RedirectToAction("PersonalOrders", "PersonalMsg");
            }
            return RedirectToAction("Login", "Login");
        }

        public void All()
        {
            var pageNamber = Convert.ToInt32(Request.Params["page"]);
            var pageSize = Convert.ToInt32(Request.Params["rows"]);
            CustomerBLL cb = new CustomerBLL();
            List<Customer> list = cb.All();
            Hashtable ht = new Hashtable
            {
               { "total", list.Count() },
               { "rows", list.Take((pageSize * pageNamber)).Skip(pageSize * (pageNamber - 1)).ToList<Customer>() }
            };
            HttpContext.Response.Write(JsonConvert.SerializeObject(ht));
            HttpContext.Response.End();
        }

        public void Select()
        {
            var pageNamber = Convert.ToInt32(Request.Params["pageNumber"]);
            var pageSize = Convert.ToInt32(Request.Params["pageSize"]);
            var text = Request.Params["selecttext"];
            var state = Convert.ToInt32(Request.Params["state"]);
            CustomerBLL cb = new CustomerBLL();
            List<Customer> list = cb.All();

            if (state != -1)
                list = (from l in list where l.State == state select l).ToList();

            if (text.Length > 0)
                list = (from l in list where l.Name.Contains(text) || l.Address.Contains(text) || l.Phone.ToString().Contains(text) select l).ToList();

            Hashtable ht = new Hashtable
            {
               { "total",list.Count() },
               { "rows", list.Take((pageSize * pageNamber)).Skip(pageSize * (pageNamber - 1)).ToList<Customer>() }
            };
            HttpContext.Response.Write(JsonConvert.SerializeObject(ht));
            HttpContext.Response.End();
        }

        public void ChangeState()
        {
            var id = Convert.ToInt32(Request.Params["ID"]);
            var state = Convert.ToInt32(Request.Params["State"]);
            CustomerBLL cb = new CustomerBLL();
            int result = 0;
            if (cb.ChangeState(id, state))
                result++;
            HttpContext.Response.Write(result);
            HttpContext.Response.End();
        }

        public void BatChangeState()
        {

            var IDs = Request.Params["IDs"].Split(',');
            var state = Convert.ToInt32(Request.Params["state"]);
            int result = 0;
            CustomerBLL cb = new CustomerBLL();
            foreach (var id in IDs)
            {
                if (!string.IsNullOrEmpty(id))
                    result = (cb.ChangeState(Convert.ToInt32(id), state)) ? result + 1 : result;
            }
            HttpContext.Response.Write(result);
            HttpContext.Response.End();
        }

        public void GetOrder(int ID)
        {
            CustomerBLL cb = new CustomerBLL();
            List<Cus_Ord> list = cb.GetOrder(ID);

            HttpContext.Response.Write(JsonConvert.SerializeObject(list));
            HttpContext.Response.End();
        }

        public ActionResult ApplyManager()
        {
            var e = (Emp)HttpContext.Session["CurrUser"];
            if (e != null)
            {
                ViewBag.CurrUser = e;
                if (e.Auth > 0)
                    return View();
                else
                    return RedirectToAction("PersonalOrders", "PersonalMsg");
            }
            return RedirectToAction("Login", "Login");
        }

        public void AllApply()
        {
            var pageNamber = Convert.ToInt32(Request.Params["page"]);
            var pageSize = Convert.ToInt32(Request.Params["rows"]);
            EmpBLL cb = new EmpBLL();
            List<Apply> list = cb.GetApply();
            Hashtable ht = new Hashtable
            {
               { "total", list.Count() },
               { "rows", list.Take((pageSize * pageNamber)).Skip(pageSize * (pageNamber - 1)) }
            };
            HttpContext.Response.Write(JsonConvert.SerializeObject(ht));
            HttpContext.Response.End();
        }

        public void Reply()
        {
            var id = Convert.ToInt16(Request.Params["id"]);
            var val = Convert.ToInt16(Request.Params["val"]);
            EmpBLL eb = new EmpBLL();
            eb.Reply(id, val);
            HttpContext.Response.Write(0);
            HttpContext.Response.End();
        }
    }
}